package org.ftz.control;
import javax.sound.sampled.TargetDataLine;
import java.io.IOException;
import java.io.OutputStream;
public class SendAudio implements Runnable {  //发送音频
    public  byte[] bos = new byte[2024];
    public  TargetDataLine targetDataLine;
    public  OutputStream outAudio;
    public SendAudio(byte[] bos, TargetDataLine targetDataLine, OutputStream outAudio) {
        this.bos = bos;
        this.targetDataLine = targetDataLine;
        this.outAudio = outAudio;
    }
    @Override
    public void run() {
        while (true) {
            if (bos != null) { //发
                int writeLen = targetDataLine.read(bos, 0, bos.length); //获取音频流
                System.out.println("server发声音"); //向对方发送拾音器获取到的音频
                try {
                    outAudio.write(bos, 0, writeLen);
                    outAudio.flush();
                } catch (IOException e) {
                    try {
                        outAudio.close();
                        break;
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                    e.printStackTrace();
                }
            }
        }
    }
}